package com.pengtu.web.project;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import com.pengtu.entity.project.OwnerOrganization;
import com.pengtu.service.project.ProjectManager;
import com.pengtu.service.project.SignOrganizationManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Results({ @Result(name = BaseActionSupport.RELOAD, type = "redirect", location = "${ctx}/project/sign/build!input.action?projectId=${projectId}&contractKind=1") })
public class BuildAction extends BaseActionSupport<OwnerOrganization> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ProjectManager projManager;
	private SignOrganizationManager signOrgManager;
	private String id;
	private OwnerOrganization ownerOrganization;
	private String projectId;
	private String contractKind;
	@Actions({
		@Action("/quality/org/build"),
		@Action("/project/sign/build")	
	})
	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}
	
	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		addActionMessage("修改" + ownerOrganization.getBurderDep() + "信息");
		signOrgManager.saveOwner(ownerOrganization);
		Struts2Utils.sendRefererUrl();
		return null;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		/*
		 * addActionMessage(sign.getStartPegNum() + "至" + sign.getEndPegNum() +
		 * "的合同被删除");
		 */projManager.deleteProj(id);
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			ownerOrganization = signOrgManager.getOwner("id", id);
		} else if (StringUtils.isNotEmpty(projectId)) {
			ownerOrganization = projManager.getProj("id", projectId)
					.getOrgCode();
		} else {
			ownerOrganization = new OwnerOrganization();
		}
	}

	public void checkOrgName() {
		// TODO Auto-generated method stub
		String oldOrgName = Struts2Utils.getParameter("oldOrgName");
		String orgName = Struts2Utils.getParameter("orgName");
		if (signOrgManager.isOrgNameUnique(orgName, oldOrgName)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
	}

	public void checkOrgNum(){
		// TODO Auto-generated method stub
		String oldOrgNum = Struts2Utils.getParameter("oldOrgNum");
		String orgNum = Struts2Utils.getParameter("orgNum");
		if (signOrgManager.isOrgNameUnique(orgNum, oldOrgNum)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
	}

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Autowired
	public void setSignOrgManager(SignOrganizationManager signOrgManager) {
		this.signOrgManager = signOrgManager;
	}

	@Autowired
	public void setProjManager(ProjectManager projManager) {
		this.projManager = projManager;
	}

	@Override
	public OwnerOrganization getModel() {
		// TODO Auto-generated method stub
		return ownerOrganization;
	}

	public String getContractKind() {
		return contractKind;
	}

	public void setContractKind(String contractKind) {
		this.contractKind = contractKind;
	}

}
